from colorama import Fore, Back, Style, init

init()

from getpass import getpass
import os
import sys
import time

from service.user_service import UserService
from service.new_service import NewService

# TEST
if __name__ == "__main__":
    u = UserService()
    result = u.login("admin", "123456")
    if result:
        print("登陆成功")
        role = u.search_user_role("admin")
        print(role)
    else:
        print("登陆失败")


__user_service = UserService()
__new_service = NewService()

while True:
    os.system("cls")
    print(Fore.LIGHTBLUE_EX, "\n\t==================")
    print(Fore.LIGHTBLUE_EX, "\t欢迎使用新闻管理系统")
    print(Fore.LIGHTBLUE_EX, "\t==================")
    print(Fore.LIGHTGREEN_EX, "\n\t1.登陆系统")
    print(Fore.LIGHTGREEN_EX, "\t2.退出系统")
    print(Style.RESET_ALL)
    opt = input("\n\t请输入操作编号: ")
    if opt == "1":
        username = input("\n\t用户名: ")
        password = getpass("\n\t密码: ")

        result = __user_service.login(username, password)
        if result:
            role = __user_service.search_user_role(username)
            print(Fore.LIGHTGREEN_EX, f"\n\t{role} 你好")
            time.sleep(1)
            while True:
                if role == "管理员":
                    print(Fore.LIGHTGREEN_EX, "\n\t1.新闻管理")
                    print(Fore.LIGHTGREEN_EX, "\n\t2.用户管理")
                    print(Fore.LIGHTRED_EX, "\n\tback.退出登陆")
                    print(Fore.LIGHTRED_EX, "\n\texit.退出系统")
                    print(Style.RESET_ALL)
                    opt = input("\n\t输入操作编号:")
                    if opt == "1":
                        while True:
                            os.system("cls")
                            print(Fore.LIGHTGREEN_EX, "\n\t1.审批新闻")
                            print(Fore.LIGHTGREEN_EX, "\n\t2.删除新闻")
                            print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                            print(Style.RESET_ALL)
                            opt = input("\n\t输入操作编号:")
                            if opt == "1":
                                page = 1
                                while True:
                                    os.system("cls")
                                    count_page = (
                                        __new_service.search_unreview_count_page()
                                    )
                                    result = __new_service.search_unreview_list(page)
                                    for index in range(len(result)):
                                        one = result[index]
                                        print(
                                            Fore.LIGHTBLUE_EX,
                                            "\n\t%d\t%s\t%s\t%s"
                                            % (index + 1, one[1], one[2], one[3]),
                                        )
                                    print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
                                    print(
                                        Fore.LIGHTBLUE_EX,
                                        "\n\t%d/%d" % (page, count_page),
                                    )
                                    print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
                                    print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                                    print(Fore.LIGHTRED_EX, "\n\tprev.上一页")
                                    print(Fore.LIGHTRED_EX, "\n\tnext.下一页")
                                    print(Style.RESET_ALL)
                                    opt = input("\n\t输入操作编号:")
                                    try:
                                        if opt == "back":
                                            break
                                        elif opt == "prev" and page > 1:
                                            page -= 1
                                        elif opt == "next" and page < count_page:
                                            page += 1
                                        elif int(opt) >= 1 and int(opt) <= 10:
                                            id = result[int(opt) - 1][0]
                                            __new_service.update_unreview_news(id)
                                    except Exception as e:
                                        print("\n\t请输入正确操作编号")
                                        time.sleep(2)

                            elif opt == "2":
                                page = 1
                                while True:
                                    os.system("cls")
                                    count_page = __new_service.search_count_page()
                                    result = __new_service.search_list(page)
                                    for index in range(len(result)):
                                        one = result[index]
                                        print(
                                            Fore.LIGHTBLUE_EX,
                                            "\n\t%d\t%s\t%s\t%s"
                                            % (index + 1, one[1], one[2], one[3]),
                                        )
                                    print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
                                    print(
                                        Fore.LIGHTBLUE_EX,
                                        "\n\t%d/%d" % (page, count_page),
                                    )
                                    print(Fore.LIGHTBLUE_EX, "\n\t-------------------")
                                    print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                                    print(Fore.LIGHTRED_EX, "\n\tprev.上一页")
                                    print(Fore.LIGHTRED_EX, "\n\tnext.下一页")
                                    print(Style.RESET_ALL)
                                    opt = input("\n\t输入操作编号:")
                                    try:
                                        if opt == "back":
                                            break
                                        elif opt == "prev" and page > 1:
                                            page -= 1
                                        elif opt == "next" and page < count_page:
                                            page += 1
                                        elif int(opt) >= 1 and int(opt) <= 10:
                                            id = result[int(opt) - 1][0]
                                            __new_service.delete_by_id(id)
                                    except Exception as e:
                                        print("\n\t请输入正确操作编号")
                                        time.sleep(2)

                            elif opt == "back":
                                break
                    elif opt == "2":
                        while True:
                            os.system("cls")
                            print(Fore.LIGHTGREEN_EX, "\n\t1.添加用户")
                            print(Fore.LIGHTGREEN_EX, "\n\t2.修改用户")
                            print(Fore.LIGHTGREEN_EX, "\n\t3.删除用户")
                            print(Fore.LIGHTRED_EX, "\n\tback.返回上一层")
                            print(Style.RESET_ALL)
                            opt = input("\n\t输入操作编号:")
                    elif opt == "back":
                        break
                    elif opt == "exit":
                        sys.exit()
                elif role == "编辑新闻":
                    print("编辑新闻")
        else:
            print(Fore.LIGHTRED_EX, "\n\t登录失败(3秒自动返回)")
            time.sleep(3)
    if opt == "2":
        sys.exit(0)
